package com.wcgksoft.controller.system;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.system.*;
import com.wcgksoft.service.SysMenuService;
import com.wcgksoft.service.SysRoleCodeService;
import com.wcgksoft.service.system.EnterpriseService;
import com.wcgksoft.service.system.SysRoleService;
import com.wcgksoft.service.system.SystemVersionService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.*;

/**
 * @author 阿良
 * @version 1.0.0
 * @ClassName SysRoleController.java
 * @Description 角色控制器
 * @createTime 2020年03月06日 15:20:00
 */

@Controller
@RequestMapping("role")
public class SysRoleController {
  @Autowired
  private SystemVersionService systemVersionService;

  @Autowired
  private SysRoleService sysRoleService;
  @Autowired
  private SysMenuService sysMenuService;
  @Autowired
  private EnterpriseService enterpriseService;
  @Autowired
  private SysRoleCodeService sysRoleCodeService;

  /**
   * @throws
   * @title TODO
   * @description 列表
   * @author 阿良
   * @param: page
   * @param: limit
   * @updateTime 2020/3/6 15:23
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "list")
  public Layui findAll(
          @RequestParam(value = "page", required = false) Integer page,
          @RequestParam(value = "limit", required = false) Integer limit) {

    DetachedCriteria criteria = DetachedCriteria.forClass(SysRole.class);
    SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
    criteria.add(Restrictions.eq("enterprise", sysUser.getEnterprise()));
    criteria.createAlias("sysRoleCode","sysRoleCode");
    criteria.add(Restrictions.ne("sysRoleCode.role_number","enterprise"));
    criteria.add(Restrictions.ne("sysRoleCode.role_number","government"));
    List<SysRole> list = sysRoleService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", sysRoleService.getTotalCount(), list);

  }


  /**
   * 添加企业下角色
   */
  @ResponseBody
  @RequestMapping(value = "update")
  public Layui update(HttpServletRequest request,
                      @RequestParam(value = "data", required = false) String data,
                      @RequestParam(value = "menu", required = false) Integer[] meun) throws Exception {
    JSONObject param = JSONObject.fromObject(data);
    if (param == null) {
      return Layui.data(1, "请求格式有误", 0, null);
    } else {
      SysRole sysRole = (SysRole) JSONObject.toBean(JSONObject.fromObject(data), SysRole.class);

      sysRole.setSysRoleCode(sysRoleCodeService.findById(param.getInt("sysRoleCode")));
      SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
      sysRole.setEnterprise(sysUser.getEnterprise());
      sysRole.setAdd_time(new Date());
      Set<SysMenu> objects = new HashSet<>();
      for (Integer meuni : meun) {
        objects.add(sysMenuService.findById(meuni));
      }
      sysRole.setSysMenuSet(objects);
      sysRoleService.saveOrUpdate(sysRole);
      return Layui.data(0, "保存成功", 1, Layui.addList(sysRole));
    }
  }


  /**
   * 删除
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "delete")
  @Transactional
  public Layui delete(@RequestParam(value = "id") Integer id) {
    System.out.println("id" + id);
    SysRole byId = sysRoleService.findById(id);
    sysRoleService.deleteCass(byId);

    return Layui.data(1, "删除成功", 1, null);
  }


  /**
   * 查询菜单权限数据
   *
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "menu")
  @Transactional
  public MsgInfo menu(@RequestParam(value = "eid") Integer eid) {
    MsgInfo info = new MsgInfo();
    Enterprise byId = enterpriseService.findById(eid);
    Set<SysMenu> sysMenuSet = byId.getSystemVersion().getSysMenuSet();
    Iterator<SysMenu> iterator = sysMenuSet.iterator();
    while (iterator.hasNext()) {
      SysMenu s = iterator.next();
      if (s.getParentId() != -1) {
        iterator.remove();
      }
    }
    for (SysMenu sysMenu : sysMenuSet) {
      if (sysMenu.getParentId() == -1) {
        DetachedCriteria criteria = DetachedCriteria.forClass(SysMenu.class);
        criteria.add(Restrictions.eq("parentId", sysMenu.getId()));
        List<SysMenu> byConditions = sysMenuService.findByConditions(criteria);
        sysMenu.setSysMenus(byConditions);
      }
    }
    info.setCode(0);
    info.setData(sysMenuSet);
    return info;
  }


  /**
   * @throws
   * @title TODO
   * @description 提供下拉框接口
   * @author 阿良
   * @updateTime 2020/3/6 15:23
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "json_role_list")
  public Layui findAll() {
    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SysRoleCode.class);
    detachedCriteria.add(Restrictions.ne("id",1));
    detachedCriteria.add(Restrictions.ne("id",2));
    detachedCriteria.add(Restrictions.ne("id",5));
    List<SysRoleCode> list = sysRoleCodeService.findByConditions(detachedCriteria);
    return Layui.data(0, "查询成功", systemVersionService.getTotalCount(), list);

  }

  @ResponseBody
  @RequestMapping(value = "json_role")
  public Layui findAllRole() {
    SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SysRole.class);
    detachedCriteria.add(Restrictions.eq("enterprise",sysUser.getEnterprise()));
    detachedCriteria.add(Restrictions.ne("sysRoleCode.id",2));
    detachedCriteria.add(Restrictions.ne("sysRoleCode.id",4));
    List<SysRole> list = sysRoleService.findByConditions(detachedCriteria);
    return Layui.data(0, "查询成功", systemVersionService.getTotalCount(), list);
  }


}
